import { FrameworkConfig } from '../extension/scripts/framework-config';

const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

/**
 * 扩展路径
 */
export const EXTENSION_PATH = path.join(__dirname, '..');

/**
 * 查找文件类型
 */
export enum FindFileType {
    FILE = 1,
    DIRECTORY = 2
}

/**
 * 在指定目录中查找文件
 * @param dir
 * @param filename
 */
export function findFile(dir: string, filename: string, types: number = FindFileType.FILE | FindFileType.DIRECTORY) {
    for (let file of fs.readdirSync(dir)) {
        let npath = path.join(dir, file);
        let info = fs.statSync(npath);
        if (file === filename && (info.isDirectory() ? types & FindFileType.DIRECTORY : types & FindFileType.FILE)) {
            return npath;
        } else if (info.isDirectory()) {
            let result = findFile(npath, filename, types);
            if (result) {
                return result;
            }
        }
    }
}

/**
 * 复制文件
 * @param srcPath
 * @param destPath
 */
export function copyFile(srcPath: string, destPath: string) {
    let destDir = path.dirname(destPath);
    if (!fs.existsSync(destDir)) {
        fs.mkdirSync(destDir, { recursive: true });
    }
    fs.copyFileSync(srcPath, destPath);
}

/**
 * 获得文件的MD5值
 * @param file
 */
export function getFileMd5(file: string) {
    return crypto.createHash('md5').update(fs.readFileSync(file)).digest('hex');
}

/**
 * 获得扩展配置
 */
const packageFile = path.join(EXTENSION_PATH, 'package.json');
let extensionConfig = null;
export function getExtensionConfig() {
    if (!extensionConfig) {
        if (fs.existsSync(packageFile)) {
            extensionConfig = JSON.parse(fs.readFileSync(packageFile, { encoding: 'utf-8' }));
        }
    }
    return extensionConfig;
}

/**
 * 获得框架配置
 */
const modulesFile = path.join(EXTENSION_PATH, 'framework', 'config.json');
let frameworkConfig: FrameworkConfig = null;
export function getFrameworkConfig() {
    if (!frameworkConfig) {
        if (fs.existsSync(modulesFile)) {
            frameworkConfig = JSON.parse(fs.readFileSync(modulesFile, { encoding: 'utf-8' }));
        }
    }
    return frameworkConfig;
}

/**
 * 获得游戏包
 */
export function getGamePacks(project: string) {
    let packs = {};
    let _collectGamePacks;
    _collectGamePacks = (path_: string) => {
        for (let f of fs.readdirSync(path_)) {
            let fpath = path.join(path_, f);
            let fstate = fs.statSync(fpath);
            if (fstate.isDirectory()) {
                let metafile = fpath + '.meta';
                if (fs.existsSync(metafile)) {
                    let meta = JSON.parse(fs.readFileSync(metafile, { encoding: 'utf-8' }));
                    if (meta?.userData?.isBundle) {
                        packs[meta.userData.bundleName || f] = path.normalize(fpath);
                        continue;
                    }
                }
                _collectGamePacks(fpath);
            }
        }
    };
    packs['main'] = path.normalize(path.join(project, 'assets'));
    _collectGamePacks(path.join(project, 'assets'));
    return packs;
}
